package com.link.base.base.position.controller;

import com.link.base.base.position.model.Position;
import com.link.base.base.position.service.PositionImpService;
import com.link.base.user.model.CoreUser;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.CallParam;
import com.link.core.basic.query.Filter;
import com.link.core.basic.service.BasicService;
import com.link.core.util.UserUtil;
import com.link.core.cllog.LCLogger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author unknown
 * @date unknown
 * @comments 职位操作处理
 */
@Controller
@RequestMapping("/link/positionImp")
public class PositionImpController extends BasicController<Position> {
    //private static final Logger logger = LogManager.getLogger(PortalPositionImpController.class);

    @Resource
    private PositionImpService positionService;

    @Override
    public BasicService<Position> getBasicService() throws Exception {
        return positionService;
    }

    @Override
    public void beforQueryAllOrExample(Position entity, HttpServletRequest request) throws Exception {
        CoreUser user = UserUtil.getUser();
        if (entity.getConditionSql() == null) {
//          entity.setConditionSql("");
        }
//      entity.setConditionSql(entity.getConditionSql()+" and createdBy="+"'"+user.getId()+"'");
        Filter filter = new Filter("createdBy", String.valueOf(user.getId()));
        List<Filter> filterList = new ArrayList<Filter>();
        filterList.add(filter);
        entity.setFilterList(filterList);
    }

    @RequestMapping(value = "/check", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> check(CallParam callParam, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            positionService.runPositionCheck(callParam);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/positionImpInsert", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> positionImpInsert(CallParam callParam, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            positionService.runNewPositionForimp(callParam);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
